home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / CmdDlgOneParam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  9.5 KB  |  327 lines  |  [TEXT/KAHL]

  1. /* CmdDlgOneParam.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "CmdDlgOneParam.h"
  31. #include "Memory.h"
  32. #include "Screen.h"
  33. #include "EventLoop.h"
  34. #include "Menus.h"
  35. #include "TextEdit.h"
  36. #include "SimpleButton.h"
  37. #include "WrapTextBox.h"
  38. #include "DataMunging.h"
  39. #include "Main.h"
  40. #include "Alert.h"
  41. #include "Numbers.h"
  42.  
  43.  
  44. #define WINXSIZE (350)
  45.  
  46. #define PROMPTX (10)
  47. #define PROMPTY (5)
  48. #define PROMPTWIDTH (WINXSIZE - (2 * PROMPTX))
  49. #define PROMPTHEIGHT (50)
  50.  
  51. #define BOXPROMPTX (PROMPTX)
  52. #define BOXPROMPTY (PROMPTY + PROMPTHEIGHT + 5)
  53.  
  54. #define BOXEDITX (BOXPROMPTX + 140)
  55. #define BOXEDITY (BOXPROMPTY - 3)
  56. #define BOXEDITWIDTH (140)
  57. #define BOXEDITHEIGHT (21)
  58.  
  59. #define CANCELBUTTONWIDTH (80)
  60. #define CANCELBUTTONHEIGHT (21)
  61. #define CANCELBUTTONX ((1 * WINXSIZE) / 4 - (CANCELBUTTONWIDTH / 2))
  62. #define CANCELBUTTONY (BOXEDITY + BOXEDITHEIGHT + 10)
  63.  
  64. #define OKBUTTONWIDTH (CANCELBUTTONWIDTH)
  65. #define OKBUTTONHEIGHT (CANCELBUTTONHEIGHT)
  66. #define OKBUTTONX ((3 * WINXSIZE) / 4 - (OKBUTTONWIDTH / 2))
  67. #define OKBUTTONY (CANCELBUTTONY)
  68.  
  69. #define WINYSIZE (CANCELBUTTONY + CANCELBUTTONHEIGHT + 20)
  70.  
  71.  
  72. typedef struct
  73.     {
  74.         WinType*                    ScreenID;
  75.         char*                            PromptText;
  76.         char*                            EditPromptText;
  77.         TextEditRec*            EditBox;
  78.         SimpleButtonRec*    OKButton;
  79.         SimpleButtonRec*    CancelButton;
  80.     } WindowRec;
  81.  
  82.  
  83. static void            RedrawWindow(WindowRec* Window)
  84.     {
  85.         CheckPtrExistence(Window);
  86.         TextEditFullRedraw(Window->EditBox);
  87.         RedrawSimpleButton(Window->OKButton);
  88.         RedrawSimpleButton(Window->CancelButton);
  89.         SetClipRect(Window->ScreenID,0,0,WINXSIZE,WINYSIZE);
  90.         DrawWrappedTextBox(Window->ScreenID,Window->PromptText,GetScreenFont(),9,
  91.             PROMPTX,PROMPTY,PROMPTWIDTH,PROMPTHEIGHT);
  92.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,Window->EditPromptText,
  93.             StrLen(Window->EditPromptText),BOXPROMPTX,BOXPROMPTY,ePlain);
  94.     }
  95.  
  96.  
  97. /* present a dialog box that allows the user to edit a single parameter */
  98. /* returns True if the user changes the value and clicks OK. */
  99. MyBoolean                CommandDialogOneParam(char* Prompt, char* BoxName, double* DataInOut)
  100.     {
  101.         WindowRec*        Window;
  102.         char*                    StringTemp;
  103.         MyBoolean            LoopFlag;
  104.         MyBoolean            DoItFlag EXECUTE(= -31342);
  105.         MyBoolean            ReturnValue;
  106.  
  107.         Window = (WindowRec*)AllocPtrCanFail(sizeof(WindowRec),
  108.             "CommandDialogOneParam:  WindowRec");
  109.         if (Window == NIL)
  110.             {
  111.              FailurePoint1:
  112.                 AlertHalt("There is not enough memory available to edit the "
  113.                     "command parameters.",NIL);
  114.                 return False;
  115.             }
  116.         Window->PromptText = Prompt;
  117.         Window->EditPromptText = BoxName;
  118.  
  119.         Window->ScreenID = MakeNewWindow(eModelessDialogWindow,eWindowNotClosable,
  120.             eWindowNotZoomable,eWindowNotResizable,DialogLeftEdge(WINXSIZE),
  121.             DialogTopEdge(WINYSIZE),WINXSIZE,WINYSIZE,(void (*)(void*))&RedrawWindow,Window);
  122.         if (Window->ScreenID == NIL)
  123.             {
  124.              FailurePoint2:
  125.                 ReleasePtr((char*)Window);
  126.                 goto FailurePoint1;
  127.             }
  128.         SetWindowName(Window->ScreenID,"Edit Command");
  129.  
  130.         Window->OKButton = NewSimpleButton(Window->ScreenID,"OK",OKBUTTONX,OKBUTTONY,
  131.             OKBUTTONWIDTH,OKBUTTONHEIGHT);
  132.         if (Window->OKButton == NIL)
  133.             {
  134.              FailurePoint3:
  135.                 KillWindow(Window->ScreenID);
  136.                 goto FailurePoint2;
  137.             }
  138.         SetDefaultButtonState(Window->OKButton,True);
  139.  
  140.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",CANCELBUTTONX,
  141.             CANCELBUTTONY,CANCELBUTTONWIDTH,CANCELBUTTONHEIGHT);
  142.         if (Window->CancelButton == NIL)
  143.             {
  144.              FailurePoint4:
  145.                 DisposeSimpleButton(Window->OKButton);
  146.                 goto FailurePoint3;
  147.             }
  148.  
  149.         Window->EditBox = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),9,
  150.             BOXEDITX,BOXEDITY,BOXEDITWIDTH,BOXEDITHEIGHT);
  151.         if (Window->EditBox == NIL)
  152.             {
  153.              FailurePoint5:
  154.                 DisposeSimpleButton(Window->CancelButton);
  155.                 goto FailurePoint4;
  156.             }
  157.         StringTemp = LongDoubleToString(*DataInOut,12,1e-6,1e6);
  158.         if (StringTemp == NIL)
  159.             {
  160.              FailurePoint6:
  161.                 DisposeTextEdit(Window->EditBox);
  162.                 goto FailurePoint5;
  163.             }
  164.         TextEditNewRawData(Window->EditBox,StringTemp,SYSTEMLINEFEED);
  165.         ReleasePtr(StringTemp);
  166.         TextEditHasBeenSaved(Window->EditBox);
  167.  
  168.  
  169.         EnableTextEditSelection(Window->EditBox);
  170.         LoopFlag = True;
  171.         while (LoopFlag)
  172.             {
  173.                 OrdType                            X;
  174.                 OrdType                            Y;
  175.                 ModifierFlags                Modifiers;
  176.                 MenuItemType*                MenuItem;
  177.                 char                                KeyPress;
  178.  
  179.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  180.                     {
  181.                         default:
  182.                             break;
  183.                         case eCheckCursor:
  184.                             if (TextEditIBeamTest(Window->EditBox,X,Y))
  185.                                 {
  186.                                     SetIBeamCursor();
  187.                                 }
  188.                              else
  189.                                 {
  190.                                     SetArrowCursor();
  191.                                 }
  192.                             goto UpdateCursorPoint;
  193.                             break;
  194.                         case eNoEvent:
  195.                          UpdateCursorPoint:
  196.                             TextEditUpdateCursor(Window->EditBox);
  197.                             break;
  198.                         case eMenuStarting:
  199.                             EnableMenuItem(mPaste);
  200.                             if (TextEditIsThereValidSelection(Window->EditBox))
  201.                                 {
  202.                                     EnableMenuItem(mCut);
  203.                                     EnableMenuItem(mCopy);
  204.                                     EnableMenuItem(mClear);
  205.                                 }
  206.                             EnableMenuItem(mSelectAll);
  207.                             if (TextEditCanWeUndo(Window->EditBox))
  208.                                 {
  209.                                     EnableMenuItem(mUndo);
  210.                                 }
  211.                             break;
  212.                         case eMenuCommand:
  213.                             if (MenuItem == mPaste)
  214.                                 {
  215.                                     TextEditDoMenuPaste(Window->EditBox);
  216.                                 }
  217.                             else if (MenuItem == mCut)
  218.                                 {
  219.                                     TextEditDoMenuCut(Window->EditBox);
  220.                                 }
  221.                             else if (MenuItem == mCopy)
  222.                                 {
  223.                                     TextEditDoMenuCopy(Window->EditBox);
  224.                                 }
  225.                             else if (MenuItem == mClear)
  226.                                 {
  227.                                     TextEditDoMenuClear(Window->EditBox);
  228.                                 }
  229.                             else if (MenuItem == mUndo)
  230.                                 {
  231.                                     TextEditDoMenuUndo(Window->EditBox);
  232.                                     TextEditShowSelection(Window->EditBox);
  233.                                 }
  234.                             else if (MenuItem == mSelectAll)
  235.                                 {
  236.                                     TextEditDoMenuSelectAll(Window->EditBox);
  237.                                 }
  238.                             else
  239.                                 {
  240.                                     EXECUTE(PRERR(AllowResume,
  241.                                         "CommandDialogOneParam: Undefined menu option chosen"));
  242.                                 }
  243.                             break;
  244.                         case eKeyPressed:
  245.                             if (KeyPress == 13)
  246.                                 {
  247.                                     FlashButton(Window->OKButton);
  248.                                     DoItFlag = True;
  249.                                     LoopFlag = False;
  250.                                 }
  251.                             else if (KeyPress == eCancelKey)
  252.                                 {
  253.                                     FlashButton(Window->CancelButton);
  254.                                     DoItFlag = False;
  255.                                     LoopFlag = False;
  256.                                 }
  257.                             else
  258.                                 {
  259.                                     TextEditDoKeyPressed(Window->EditBox,KeyPress,Modifiers);
  260.                                 }
  261.                             break;
  262.                         case eMouseDown:
  263.                             if (SimpleButtonHitTest(Window->OKButton,X,Y))
  264.                                 {
  265.                                     if (SimpleButtonMouseDown(Window->OKButton,X,Y,NIL,NIL))
  266.                                         {
  267.                                             DoItFlag = True;
  268.                                             LoopFlag = False;
  269.                                         }
  270.                                 }
  271.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  272.                                 {
  273.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  274.                                         {
  275.                                             DoItFlag = False;
  276.                                             LoopFlag = False;
  277.                                         }
  278.                                 }
  279.                             else if (TextEditHitTest(Window->EditBox,X,Y))
  280.                                 {
  281.                                     TextEditDoMouseDown(Window->EditBox,X,Y,Modifiers);
  282.                                 }
  283.                             break;
  284.                     }
  285.             }
  286.         ERROR((DoItFlag != True) && (DoItFlag != False),PRERR(ForceAbort,
  287.             "CommandDialogOneParam:  DoItFlag is neither true nor false"));
  288.  
  289.         ReturnValue = False;
  290.  
  291.         if (DoItFlag)
  292.             {
  293.                 MyBoolean                    ErrorNoMemory;
  294.  
  295.                 ErrorNoMemory = False;
  296.  
  297.                 if (TextEditDoesItNeedToBeSaved(Window->EditBox))
  298.                     {
  299.                         StringTemp = TextEditGetRawData(Window->EditBox,SYSTEMLINEFEED);
  300.                         if (StringTemp == NIL)
  301.                             {
  302.                                 ErrorNoMemory = True;
  303.                             }
  304.                          else
  305.                             {
  306.                                 *DataInOut = StringToLongDouble(StringTemp,PtrSize(StringTemp));
  307.                                 ReleasePtr(StringTemp);
  308.                                 ReturnValue = True;
  309.                             }
  310.                     }
  311.  
  312.                 if (ErrorNoMemory)
  313.                     {
  314.                         AlertHalt("There was not enough memory available to save all of "
  315.                             "the attributes.",NIL);
  316.                     }
  317.             }
  318.  
  319.         DisposeSimpleButton(Window->OKButton);
  320.         DisposeSimpleButton(Window->CancelButton);
  321.         DisposeTextEdit(Window->EditBox);
  322.         KillWindow(Window->ScreenID);
  323.         ReleasePtr((char*)Window);
  324.  
  325.         return ReturnValue;
  326.     }
  327.